// pages/login/index.js
const AJAX = require("../../common/request")
const API = require("../../common/api")
const common = require("../../common/common")
const App = getApp()
Page({
/**
 * 页面的初始数据
 */
data: {
    loginShow: true,
    userInfoShow: false,
    //sessionKey:"",
    wxCode:"",
    userId:"",
    avatarUrl: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    nickname: "微信用户",
    userPhone: "",
    navTop: 0,
    navContentHeight: 0,
    placeholderHeight: 0,
    temp_val:"",
    radio: false,
		APPLETSTATUS: App.globalData.APPLETSTATUS
},
    jump2FromPage(){
        const pages = getCurrentPages();
        let delta = 0
        //安卓手机bug 页面被加载2次
        for (let i = pages.length; i > 0; i--) {
            if (pages[i - 1].route != "pages/login/index") {
                break;
            }
            delta = delta + 1
        }
        wx.navigateBack({
            delta: delta > 0 ? delta : 1
        })
    },
    changeRegisterView(){
        if(this.data.loginShow){
            this.setData({
                loginShow: false,
                userInfoShow: true,
            })
        }else{
            this.setData({
                loginShow: true,
                userInfoShow: false,
            })
        }
    },
onChooseAvatar(e) {
    console.log("onChooseAvatar", e)
    // 执行头像上传
    AJAX.UPLOADFILE("/system/AppWechat/addfile", e.detail.avatarUrl, {

    }).then(res => {
        // 图片地址赋值回显
        let result = JSON.parse(res.data)
        let imgUrl = API.UPLOADURL + result.msg
        console.log("图片地址赋值回显", imgUrl)
        this.setData({
            avatarUrl: imgUrl
        })
    })
},
    userInfoSubmit(e){
        console.log(e);
        AJAX.POST("/wxLogin/wxUserRegisterUpdate",{
            userId: this.data.userId,
            nickname: e.detail.value.nickname,
            avatarUrl: this.data.avatarUrl
        }).then(res=>{
            console.error("-- wxUserRegisterUpdate :",res)
            let data = res.data;
            if(res.code !== 200){
                console.error("-- wxUserRegisterUpdate error :",res)
                this.jump2FromPage();
                return
            }
            wx.setStorageSync('wxLoginOpenid', data.openId) // 登录状态
            wx.setStorageSync('wxLoginStatus', true) // 登录状态
            wx.setStorageSync('wxLoginImg', data.headImg)
            App.globalData.user.wxLoginImg = data.headImg
            wx.setStorageSync('wxLoginUsername', data.wechatUserName)
            App.globalData.user.wxLoginUsername = data.wechatUserName
            App.globalData.user.wxLoginOpenid = data.openId
            App.globalData.user.wxLoginStatus = true
            // 切换画面
            //this.jump2FromPage();
            wx.navigateTo({
                url: '/pages/chat/index/index',
                // url: '/pages/chat/webView/index'
            })
        }).catch(res=>{
            console.error("-- wxUserRegisterUpdate error :",res)
            this.jump2FromPage();
        });

    },
/**
 * 生命周期函数--监听页面加载
 */
onLoad(options) {
    this.setData({
        navTop: App.globalData.navTop,
        navContentHeight: App.globalData.navContentHeight,
    })
    const query = wx.createSelectorQuery();
    query.select('.head').boundingClientRect();
    query.exec((res) => {
        this.setData({
            placeholderHeight: res[0].height
        })
    });
},
// 获取手机号
// 使用防抖节流控制调用次数
checkRadio() {
    let radio = this.data.radio == false ? true : false;
    this.setData({
        radio: radio,
    })
},
toContract() {
    wx.navigateTo({
        url: '/pages/login/contract/contract',
    })
},
onShareAppMessage(options) {
    // 默认分享配置
    return {
        title: App.globalData.onShareAppMessage.title,
        path: App.globalData.onShareAppMessage.path,
        imageUrl: App.globalData.onShareAppMessage.imageUrl,
    }
},
loginTip() {
    if (!this.data.radio) {
        wx.showModal({
            title: '提示信息',
            content: '请勾选用户隐私协议',
            showCancel: false,
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {

                }
            }
        })
    }
},
    bindGetUserInfo:function(e){
        console.log("bindGetUserInfo -- :" ,e)
    },
getPhoneNumber: common.throttle(function (e) {
    if (!this.data.radio) {
        wx.showModal({
            title: '提示信息',
            content: '请勾选用户隐私协议',
            showCancel: false,
            complete: (res) => {
                if (res.cancel) {

                }

                if (res.confirm) {

                }
            }
        })
        return
    }
    let this_ = this;
    wx.login({
        success: (res) => {
            console.log("wx login : ",res)
            this_.data.wxCode = res.code
            let this__ = this_;
            AJAX.GET("/wxLogin/wxUserRegister", {
                loginCode:res.code,
                code:e.detail.code,
            }).then(res => {
                console.log("wxUserRegister : ",res)
                if(res.code !== 200){
                    console.error("-- wxUserRegister error :",res)
                    this.jump2FromPage();
                    return
                }
                //this__.data.sessionKey = res.data.sessionKey;
                this__.data.userId = res.data.wechatUserId;
                let data = res.data;
                wx.setStorageSync('wxLoginPhone', data.phone)
                wx.setStorageSync('wxLoginUsername', data.wechatUserName)
                wx.setStorageSync('wxLoginImg', data.headImg)
                wx.setStorageSync('wxUserId', data.wechatUserId)
                wx.setStorageSync('wechatUserId', data.wechatUserId)
                App.globalData.user.wxLoginUsername = data.wechatUserName
                App.globalData.user.wxUserId = data.wechatUserId
                App.globalData.user.wxLoginPhone = data.phone
                App.globalData.user.wxLoginImg = data.headImg
                App.globalData.user.wechatUserId = data.wechatUserId
                if(data.status ===1){
                    wx.setStorageSync('wxLoginOpenid', data.openId) // 登录状态
                    wx.setStorageSync('wxLoginStatus', true) // 登录状态
                    wx.setStorageSync('wxLoginImg', data.headImg)
                    App.globalData.user.wxLoginImg = data.headImg
                    wx.setStorageSync('wxLoginUsername', data.wechatUserName)
                    App.globalData.user.wxLoginUsername = data.wechatUserName
                    App.globalData.user.wxLoginOpenid = data.openId
                    App.globalData.user.wxLoginStatus = true
                    wx.navigateTo({
                        url: '/pages/chat/index/index',
                        // url: '/pages/chat/webView/index'
                    })
                    return;
                }
                // 切换头像与昵称界面
                this.setData({
                    loginShow: false,
                    userInfoShow: true,
                })
            }).catch(res =>{
                console.error("-- wxUserRegister error :",res)
                this.jump2FromPage();
            });
        }
    })
})
})